package com.vf.admin.client.controller;

import cn.hutool.core.util.ObjectUtil;
import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.service.AppSetupService;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.miaodong.entity.AppSetup;
import com.vf.admin.utils.ip.IpConstant;
import com.vf.admin.utils.ip.IpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 配置信息
 *
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppSetupController.java
 * @Description TODO
 * @createTime 2021年07月27日 15:11:00
 */
@CrossOrigin
@Controller
@RequestMapping("/app/setup")
public class AppSetupController extends ClientBaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AppSetupService appSetupService;

    /**
     * 获取页面开关
     */
    @RequestMapping("/iosUrl")
    @ResponseBody
    public Result iosUrl() {
        try {
            Map<String, Object> map = new HashMap<>();
            AppSetup appSetup = appSetupService.get();
            map.put("iosOneUrl", appSetup.getIosOneUrl()); //混淆第一个界面
            map.put("iosTwoUrl", appSetup.getIosTwoUrl()); //混淆第二个界面
            map.put("iosThreeUrl", appSetup.getIosThreeUrl()); //混淆第三个界面
            map.put("iosUrlButton", appSetup.getIosUrlButton()); // ios按钮1假页面 2真实
            map.put("iosUserInfo", appSetup.getUserInfoSwitch());  //手机号1隐藏 2 vip付费 3钻石
            map.put("vipManage", appSetup.getVipManage());   //vip开关 1开启 2 关闭
            map.put("giftButton", appSetup.getGiftButton()); //1关 2 开
            map.put("userInfoSeePrice", appSetup.getUserInfoSeePrice()); //查看用户信息费用
            //ip判断
            String ipAddress = IpUtils.getIpAddress(request);
            if (ObjectUtil.isNotEmpty(ipAddress)){
                map.put("ipFlag",false);
            }else {
                Map<String, Object> ip = IpConstant.ip;
                boolean b = IpUtils.ipInChina(ip, ipAddress);
                map.put("ipFlag",b);
            }
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }


    /**
     * 获取vip开关
     */
    @RequestMapping("/custom")
    @ResponseBody
    public Result custom() {
        try {
            Map<String, Object> map = new HashMap<>();
            AppSetup appSetup = appSetupService.get();
            map.put("kefuUrl", appSetup.getKefuUrl()); //客服
            map.put("vipManage", appSetup.getVipManage()); //vip开关 1开启 2 关闭
            map.put("userSwitch", appSetup.getUserInfoSwitch());//手机号1隐藏 2 vip付费 3钻石
            map.put("userInfoSeePrice", appSetup.getUserInfoSeePrice()); //查看用户信息费用
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    /**
     * 全部信息
     */
    @RequestMapping("/quanbuxinxi")
    @ResponseBody
    public Result quanbuxinxi() {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            map.put("quanbuxinxi", appSetup);
            return this.sucessJSON(appSetup);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    /**
     * 用户协议信息
     *
     * @Param: []
     * @return: com.vf.admin.common.entity.Result
     * @Author: zhouyunyi
     * @Date: 2021/10/21
     */
    @RequestMapping("/yonghuxieyi")
    @ResponseBody
    public Result xieyi() {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            map.put("yonghuxieyi", appSetup.getUseragreement());
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    /**
     * 隐私协议信息
     *
     * @Param: []
     * @return: com.vf.admin.common.entity.Result
     * @Author: zhouyunyi
     * @Date: 2021/10/21
     */
    @RequestMapping("/yinsixieyi")
    @ResponseBody
    public Result yinsixieyi() {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            map.put("yinsixieyi", appSetup.getPrivacyprotocol());
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }


    /**
     * 用户协议信息
     *
     * @Param: []
     * @return: com.vf.admin.common.entity.Result
     * @Author: zhouyunyi
     * @Date: 2021/10/21
     */
    @RequestMapping("/yonghuxieyis")
    @ResponseBody
    public Result xieyis() {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            map.put("yonghuxieyis", appSetup.getUseragreements());
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    /**
     * 隐私协议信息
     *
     * @Param: []
     * @return: com.vf.admin.common.entity.Result
     * @Author: zhouyunyi
     * @Date: 2021/10/21
     */
    @RequestMapping("/yinsixieyis")
    @ResponseBody
    public Result yinsixieyis() {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            map.put("yinsixieyis", appSetup.getPrivacyprotocols());
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }


    @RequestMapping("/authagreement")
    @ResponseBody
    public Result authagreement() {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            map.put("authagreement", appSetup.getAuthagreement());
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    @RequestMapping("/invitegreement")
    @ResponseBody
    public Result invitegreement() {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            map.put("invitegreement", appSetup.getInvitegreement());
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    @RequestMapping("/charmgreement")
    @ResponseBody
    public Result charmgreement() {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            map.put("charmgreement", appSetup.getCharmgreement());
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    @RequestMapping("/platformConductStandard")
    @ResponseBody
    public Result platformConductStandard() {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            map.put("text", appSetup.getPlatformConductStandard());
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }


    @RequestMapping("/platformConductStandards")
    @ResponseBody
    public Result platformConductStandards() {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            map.put("texts", appSetup.getPlatformConductStandards());
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    @RequestMapping("/liveServiceAgreement")
    @ResponseBody
    public Result liveServiceAgreement() {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            map.put("text", appSetup.getLiveServiceAgreement());
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    @RequestMapping("/liveManagementStandard")
    @ResponseBody
    public Result liveManagementStandard() {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            map.put("text", appSetup.getLiveManagementStandard());
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    @RequestMapping("/gzhImage")
    @ResponseBody
    public Result gzhImage() {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            map.put("image", appSetup.getGzhImage());
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    @RequestMapping("/writeOff")
    @ResponseBody
    public Result writeOff() {
        try {
            Integer userId = getUserId();
            String key = String.format(RedisKeys.WRITE_OFF, userId);
            redisUtil.set(key, Constants.RedisKeysLife.hours_12);
            return this.sucessJSON("操作成功，12小时后本账号将自动注销！");
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    /**
     * 文明公约
     *
     * @return
     */
    @RequestMapping("/wmAgreement")
    @ResponseBody
    public Result wmAgreement() {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            map.put("text", appSetup.getWmAgreement());
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    @RequestMapping("/rechargeAgreement")
    @ResponseBody
    public Result rechargeAgreement() {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            map.put("text", appSetup.getRechargeagreement());
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取协议信息异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    @RequestMapping("/iosModuleStatus")
    @ResponseBody
    public Result iosModuleStatus(String version) {
        try {
            Map<String, Object> map = new HashMap<>(16);
            AppSetup appSetup = appSetupService.get();
            Integer iosProdVersion = Integer.valueOf(appSetup.getIosVersion().replace(".", ""));
            Integer auditVersion = Integer.valueOf(version.replace(".", ""));
            if (auditVersion > iosProdVersion) {
                map.put("iosModuleStatus", appSetup.getIosModuleStatus());
            } else {
                map.put("iosModuleStatus", 0);
            }
            return this.sucessJSON(map);
        } catch (Exception e) {
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    /**
     * ios 支付开关
     *
     * @return
     */
    @RequestMapping(value = "/newsFlag", method = RequestMethod.POST)
    @ResponseBody
    public Result iosPayFlag() {
        try {
            AppSetup setup = appSetupService.get();
            Map<String, String> map = new HashMap<>();
            map.put("newsFlag", setup.getIosPayFlag());
           /* map.put("newsUrl", setup.getH5PayUrl());
            map.put("vipNewsUrl", setup.getH5VipPayUrl());*/
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取IOS支付开关异常:", e);
            return this.errorJSON("服务器繁忙，请您稍后重试!");
        }
    }

}
